package com.grade.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.grade.common.core.domain.entity.SysDictData;
import com.grade.common.core.page.DynamicTableDataInfo;
import com.grade.common.core.text.Convert;
import com.grade.system.domain.*;
import com.grade.system.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.grade.common.annotation.Log;
import com.grade.common.enums.BusinessType;
import com.grade.common.core.controller.BaseController;
import com.grade.common.core.domain.AjaxResult;
import com.grade.common.utils.poi.ExcelUtil;
import com.grade.common.core.page.TableDataInfo;

/**
 * 评分模板Controller
 * 
 * @author grade
 * @date 2021-10-17
 */
@Controller
@RequestMapping("/system/template")
public class GradeTemplateController extends BaseController
{
    private String prefix = "system/template";

    @Autowired
    private IGradeTemplateService gradeTemplateService;
    @Autowired
    private IGradeTypeService gradeTypeService;

    @Autowired
    private IGradeItemService gradeItemService;
    @Autowired
    private IItemTemplateRelationService itemTemplateRelationService;
    @Autowired
    private ISysDictDataService dictDataService;

    @RequiresPermissions("system:template:view")
    @GetMapping()
    public String template()
    {
        return prefix + "/template";
    }

    /**
     * 查询评分模板列表
     */
    @RequiresPermissions("system:template:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GradeTemplate gradeTemplate)
    {
        startPage();
        List<GradeTemplate> list = gradeTemplateService.selectGradeTemplateList(gradeTemplate);
        return getDataTable(list);
    }

    /**
     * 导出评分模板列表
     */
    @RequiresPermissions("system:template:export")
    @Log(title = "评分模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GradeTemplate gradeTemplate)
    {
        List<GradeTemplate> list = gradeTemplateService.selectGradeTemplateList(gradeTemplate);
        ExcelUtil<GradeTemplate> util = new ExcelUtil<GradeTemplate>(GradeTemplate.class);
        return util.exportExcel(list, "评分模板数据");
    }

    /**
     * 新增评分模板
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/getPeriodType")
    @ResponseBody
    public AjaxResult  getPeriodType(GradeTemplate gradeTemplate)
    {
        List<SysDictData> periodList = dictDataService.selectPeriodByCity("b_period_type", gradeTemplate.getCity());
        return success(periodList);
    }

    @GetMapping("/getTemplate")
    @ResponseBody
    public AjaxResult getTemplate(GradeTemplate gradeTemplate)
    {
        //status默认为可用的
        gradeTemplate.setStatus("0");
        List<GradeTemplate> list = gradeTemplateService.selectGradeTemplateList(gradeTemplate);
        return success(list);
    }

    /**
     * 新增保存评分模板
     */
    @RequiresPermissions("system:template:add")
    @Log(title = "评分模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GradeTemplate gradeTemplate)
    {
        return toAjax(gradeTemplateService.insertGradeTemplate(gradeTemplate));
    }

    /**
     * 新增保存评分模板
     */
    @Log(title = "评分模板", businessType = BusinessType.INSERT)
    @PostMapping("/getTemplateById")
    @ResponseBody
    public AjaxResult getTemplateById(GradeTemplate gradeTemplate)
    {
        String[]columns = {"firstColumn","secondColumn","threeColumn","fourColumn","fiveColumn","sixColumn"};
        String[]titles = {"一级类型","二级类型","三级类型","四级类型","五级类型","六级类型"};
        List<TableColumn>columnList = new ArrayList<>();
        GradeTemplate template = gradeTemplateService.selectGradeTemplateById(gradeTemplate.getId());
        if(template!=null){
            for(int i=0;i<template.getLayer();i++ ){
                TableColumn tableColumn = new TableColumn(titles[i],columns[i],"150");
                columnList.add(tableColumn);
            }
        }
        TableColumn contentColumn = new TableColumn("评分细则","itemContent");
        TableColumn scoreColumn = new TableColumn("封顶分数","maxScore","50");
        TableColumn weight = new TableColumn("权重","weight","50");
        columnList.add(contentColumn);
        columnList.add(scoreColumn);
        columnList.add(weight);
        return  AjaxResult.success(columnList);
    }
    /**
     * 新增保存评分模板
     */
    @Log(title = "评分模板", businessType = BusinessType.INSERT)
    @PostMapping("/getItemsByTemplateId")
    @ResponseBody
    public TableDataInfo getItemsByTemplateId(GradeItem gradeItem)
    {
        List<GradeItem> gradeItemList =  gradeItemService.selectGradeItemList(gradeItem);
        //计算可以输入的最高最低分
        if(gradeItemList!=null && gradeItemList.size()>0){
            for(GradeItem item: gradeItemList){
                Integer maxScore = item.getMaxScore();
                if(maxScore!=null && maxScore>=0){
                    item.setInputMax(maxScore);
                    item.setInputMin(0);
                }else{
                    item.setInputMax(0);
                    item.setInputMin(maxScore);
                }
            }
        }
        return getDataTable(gradeItemList);
    }


    /**
     * 修改评分模板
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GradeTemplate gradeTemplate = gradeTemplateService.selectGradeTemplateById(id);
        mmap.put("gradeTemplate", gradeTemplate);
        return prefix + "/edit";
    }

    /**
     * 修改保存评分模板
     */
    @RequiresPermissions("system:template:edit")
    @Log(title = "评分模板", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GradeTemplate gradeTemplate)
    {
        return toAjax(gradeTemplateService.updateGradeTemplate(gradeTemplate));
    }

    /**
     * 查询细则详细
     */
    @GetMapping("/item/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        mmap.put("id", id);
        return "system/dict/data/data";
    }


    /**
     * 删除评分模板
     */
    @RequiresPermissions("system:template:remove")
    @Log(title = "评分模板", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int typeCount = gradeTypeService.countGradeTypeByTemplateIds(Convert.toStrArray(ids));
        if(typeCount>0){
            return error("该模板下存在分类，不能删除");
        }
        return toAjax(gradeTemplateService.deleteGradeTemplateByIds(ids));
    }

    /**
     * 状态修改
     */
    @Log(title = "状态管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("system:template:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(GradeTemplate gradeTemplate)
    {
        return toAjax(gradeTemplateService.updateGradeTemplate(gradeTemplate));
    }

}
